#include "userhandle.h"
#include <QDir>
#include <QMessageBox>

userHandle::userHandle(QObject *parent)
    : QObject{parent}
{
    this->m_camera = new opencv_camera();
    this->m_imgPv = new MyImageProvider();
    this->m_imgPv->setCameraInstance(this->m_camera);
    this->m_directshow = new mydirectshow();
    this->m_file = new file();
}

MyImageProvider* userHandle::getImageProvider()
{
    return this->m_imgPv;
}

bool userHandle::openCamera(int devID)
{
    return this->m_camera->openCameraDev(0);
}

void userHandle::closeCamera()
{
    this->m_camera->closeCameraDev();
}

QStringList userHandle::getCameraNameList()
{
    return this->m_directshow->getCameraNameList();
}
// 打开根目录路径
bool userHandle::openRootFileDir()
{
    bool ret = false;

    ret = this->m_file->openFileDirectory();
    if(ret) emit rootFileDirectoryOpened(this->m_file->getFilePath(), this->m_file->getFileList());

    return ret;
}
// 获取最内层目录 图片文件
QStringList userHandle::getDirEntryList(QString path)
{
    QDir dir;
    QStringList fileList;
    QStringList nameFilters;

    nameFilters << "*.jpg" << "*.png" << "*.bmp";
    dir.setPath(path);
    fileList = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);

    return fileList;
}
// 保存CSV文件 txt格式
bool userHandle::saveCSVFile()
{
    bool ret = false;
    QString text;
    QStringList fileList;

    text.clear();
    for(int i = 0; i < this->m_file->getFileList().count(); i++) {
        QString filePath = QString("%1/%2").arg(this->m_file->getFilePath()).arg(QString(this->m_file->getFileList().at(i)));
        int num = QString(this->m_file->getFileList().at(i)).remove(0,1).toInt();
        fileList = getDirEntryList(filePath);
        for(int j = 0; j < fileList.count(); j++) {
            text.append(QString("%1/%2;%3\n").arg(filePath).arg(QString(fileList.at(j))).arg(num));
        }
    }
    ret = this->m_file->saveFileTxt(text);

    return ret;
}
// 打开需要预处理的任一图片
bool userHandle::openPictureFile()
{
    bool ret = false;

    ret = this->m_file->openImageFile();
    if(ret) emit pictureOpened(this->m_file->getFilePath(), this->m_file->getFileList());

    return ret;
}
// 检测图片中是否存在人脸
bool userHandle::checkPicture_face(QString path)
{
    if(!path.first(2).contains(":")) return false;
    int faceNum = 0;

    faceNum = this->m_camera->checkPicture_face(path);
    emit checkPictureOvered(faceNum);

    return true;
}
// 选择拍照图片保存路径
bool userHandle::openPhotoFileDir()
{
    bool ret = false;

    ret = this->m_file->openFileDirectory();
    if(ret) emit photoFileDirectoryOpened(this->m_file->getFilePath());

    return ret;
}
// 保存拍照图片到指定路径
bool userHandle::savePhotoFile(QString path)
{
    if(!path.first(2).contains(":")) return false;
    bool ret = false;
    static int num = 1;

    ret = this->m_camera->saveCameraImage(QString("%1%2.jpg").arg(path).arg(num++));

    return ret;
}
// 选择预处理保存路径
bool userHandle::openRetreatmentFileDir()
{
    bool ret = false;

    ret = this->m_file->openFileDirectory();
    if(ret) emit retreatmentDirectoryOpened(this->m_file->getFilePath());

    return ret;
}
// 预处理图片 图片大小92*112
bool userHandle::retreatmentPicture(QString readPath, QString savePath)
{
    if(!readPath.first(2).contains(":") || !savePath.first(2).contains(":")) return false;
    bool ret = false;

    ret = this->m_camera->resizeStdPicture(readPath, QString("%1%2").arg(savePath.split(".").constFirst()).arg(".bmp"));

    return ret;
}
// 预处理图片 一键处理
bool userHandle::retreatmentPictureAll(QString readPath, QString savePath, QStringList pictureList)
{
    if(!readPath.first(2).contains(":") || !savePath.first(2).contains(":")) return false;
    bool ret = false;

    for(int i = 0; i < pictureList.size(); i++)
    {
        ret = this->m_camera->resizeStdPicture(QString("%1%2").arg(readPath).arg(QString(pictureList.at(i))), QString("%1%2%3").arg(savePath).arg(QString(pictureList.at(i)).split(".").constFirst()).arg(".bmp"));
        if(ret == false) break;
    }
    if(ret) emit resizeAllPictureOvered();

    return ret;
}
// 获取图片的尺寸大小
QSize userHandle::getPictureSize(QString path)
{
    if(!path.first(2).contains(":")) return QSize(0,0);

    return this->m_camera->getPictureSize(path);
}

bool userHandle::saveRecognizerFile(QString csvPath, QString savePath, QString fileName, int mode)
{
    if(!csvPath.first(2).contains(":") || !savePath.first(2).contains(":")) return false;
    if(fileName.length() < 1) return false;
    return this->m_camera->createRecognizerXMLFile(csvPath, savePath, fileName, mode);
}
// 选择csv文件
bool userHandle::openCSVFile()
{
    bool ret = false;

    ret = this->m_file->openTxtFile();
    if(ret) emit csvFileDirectoryOpened(this->m_file->getFilePath() + this->m_file->getFileName());

    return ret;
}
// 选择识别器模型保存路径
bool userHandle::openRecognizerFileDir()
{
    bool ret = false;

    ret = this->m_file->openFileDirectory();
    if(ret) emit recognizerDirectoryOpened(this->m_file->getFilePath());

    return ret;
}
// 选择XML文件
bool userHandle::openRecognizerFile()
{
    bool ret = false;

    ret = this->m_file->openXmlFile();
    if(ret) emit xmlFileDirectoryOpened(this->m_file->getFilePath() + this->m_file->getFileName());

    return ret;
}
// 设置人脸识别器模型
bool userHandle::setFaceRecognizer(QString path, int mode)
{
    if(!path.first(2).contains(":")) return false;

    this->m_camera->setFaceRecognizer(path, mode);

    return true;
}

void userHandle::warningWindow(QString title, QString text)
{
    QMessageBox::warning(NULL, title, text);
}
